﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Ccit.Utils;
using Complus_DataAccess;
using Ccit.ClassLibrary;

namespace Ccit.SysPlat
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            //string pduCode = ConfigLib.GetAttribute(eNodeType.PDUCODE);
            //string pduNumber = ConfigLib.GetAttribute(eNodeType.PDUNUMBER);
            string pduCode = string.Empty;
            string pduNumber = string.Empty;
            ConfigLib.GetPduIdentify(ref pduCode, ref pduNumber);
            if (string.IsNullOrEmpty(pduCode) || string.IsNullOrEmpty(pduNumber))
            {
                Response.Write("<font color='red'>缺少产品标识信息,系统可能没有进行正确配置!请检查!</font>");
                Response.End();
                return;
            }

            Application["PduIdentify"] = "<IDENTIFY><PDUCODE><![CDATA[" + pduCode +
                                         "]]></PDUCODE><REGNUMBER><![CDATA[" + pduNumber + "]]></REGNUMBER></IDENTIFY>";

            #region logout code (tim)
            //            Application["PduIdentify"] = @"<IDENTIFY>
            //                                             <PDUCODE>MOA</PDUCODE>  
            //                                             <REGNUMBER>0</REGNUMBER>
            //                                           </IDENTIFY>";
            #endregion

            #region logout code (tim 2010-02-04) 获取数据库类型配置，需要调用一个.Net接口此处替换COM+接口
            DataAccessClass comDataAccess = new DataAccessClass();
            string sPduIdentify = HttpContext.Current.Application["PduIdentify"].ToString();
            Application["DBType"] = comDataAccess.getDataBaseType(sPduIdentify);
            Ccit.Utils.Common.ReleaseComObject(comDataAccess);
            #endregion
            
            //Application["DBType"] = "4";

            Application["MultiDept"] = ConfigLib.GetAttribute(eNodeType.MULTIDEPT);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session["LogUserInfo"] = null;
            Session.Timeout = 1600;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            string strURL = Request.RawUrl;
            int questIndex = strURL.IndexOf("?");

            string reqPage = string.Empty;
            string param = string.Empty;
            string encodeURL = string.Empty;

            if (questIndex != -1)
            {
                reqPage = strURL.Substring(0, questIndex + 1);
                int pageType = reqPage.LastIndexOf("aspx");
                if (pageType != -1)
                {
                    param = strURL.Substring(questIndex + 1);

                    int typeParmIndex = param.ToLower().IndexOf("at=1");
                    if (typeParmIndex == -1)
                    {
                        try
                        {
                            param = Base64Lib.Decode(param);  // attemp to decode url
                            encodeURL = reqPage + param;
                            HttpContext.Current.RewritePath(encodeURL);
                        }
                        catch  // fault then use origin url
                        {
                            Response.Write("<font color='red'>非法的Url请求,请与管理员联系!</font>");
                            Response.End();
                        }
                    }
                }
            }
        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            #region logout code tim 2010-03-31
            //MsLoginUser loginUser = Session["LogUserInfo"] as MsLoginUser;
            //if (null != loginUser)
            //{
            //    string dbType = Application["DBType"] as string;
            //    /* 代码注释:
            //     *     此时已不存在上下文环境,不能通过工厂类来实现接口对象，
            //     *     因此手工实现接口对象 (tim 2009-05-20) */
            //    IUser _userProcessor = null;
            //    if (dbType == "1")
            //    {
            //        _userProcessor = new UserSQL();
            //    }
            //    else if (dbType == "4")
            //        _userProcessor = new UserAES();


            //    List<string> lstResult = _userProcessor.SetUserOnlineStatus(loginUser, "0");
            //    loginUser = null;
            //    _userProcessor = null;
            //}
            #endregion

            Session.Abandon();
            Session["LogUserInfo"] = null;            
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Application["PduIdentify"] = "";
            Application["DBType"] = "";
            Application["MultiDept"] = "";
        }
    }
}